/* eslint-disable no-debugger */
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable jsx-a11y/label-has-associated-control */
/**
 * 表格操作列单元格
 * @author LiAnqi
 * @Date 2022.6.1
 */
import { Space, Button } from 'antd'
import ButtonIcon from '../../fc-icon/ButtonIcon'
import './ActionCell.less'

function ActionCell(props) {
  const { elements = [], record, index } = props

  const elementRender = (element, eIndex) => {
    const { text, type, icon, onClick } = element
    const handleClick = () => {
      onClick(record, index)
    }
    // type 允许是函数
    switch (typeof type === "function" ? type(record) : type) {
      case 'button':
        return (
          <Button icon={<ButtonIcon iconType={icon} />} onClick={handleClick} key={eIndex}>
            {text}
          </Button>
        )
      case 'link':
        return (
          <Button onClick={handleClick} className="link-button" key={eIndex}>
            {text}
          </Button>
        )
      case 'text':
        return text
      default:
        return <span key={eIndex}></span>
    }
  }

  const renderCell = () => {
    const dom = []
    elements?.forEach((element, eIndex) => {
      dom.push(elementRender(element, eIndex))
    })
    return dom
  }

  return <Space>{renderCell()}</Space>
}

export default ActionCell
